<template>
  <el-dialog
    title="选择商品(点击行即可选择)"
    :visible.sync="openChooseGoods"
    width="80%"
    append-to-body
  >
    <el-form
      :model="queryParams"
      ref="queryForm"
      :inline="true"
      v-show="showSearch"
      label-width="80px"
    >
      <el-form-item label="产品名称" prop="goodsName">
        <el-input
          v-model="queryParams.goodsName"
          placeholder="请输入产品名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>
    <el-table
      v-loading="loading"
      :data="goodsList"
      highlight-current-row
      @current-change="handleCurrentChange"
      @selection-change="handleSelectionChange"
      ref="singleTable"
    >
      <el-table-column
        v-if="multiple"
        type="selection"
        width="45">
      </el-table-column>
      <el-table-column label="产品编号" align="center" prop="goodsNumber"/>
      <el-table-column label="规格" align="center" prop="desc" v-if="goodsStandard"/>
      <el-table-column label="产品封面" align="center" prop="goodsCover">
        <template slot-scope="scope">
          <el-image
            style="width:40px;height:40px"
            fit="cover"
            :src="scope.row.goodsCover[0]"
            :preview-src-list="scope.row.goodsCover"
          ></el-image>
        </template>
      </el-table-column>
      <el-table-column label="产品名称" align="center" prop="goodsName"/>
      <el-table-column label="产品价格" align="center" prop="goodsMoneyMin">
        <template slot-scope="scope">
          <span v-if="!goodsStandard">{{scope.row.goodsMoneyMin}}元~{{scope.row.goodsMoneyMax}}元</span>
          <span v-else>{{scope.row.price}}元</span>
        </template>
      </el-table-column>
      <el-table-column label="产品状态" align="center" prop="goodsStatus">
        <template slot-scope="scope">
          <span>{{scope.row.goodsStatus | filterGoodsStatus}}</span>
        </template>
      </el-table-column>
      <el-table-column label="创建时间" align="center" prop="createTime"/>
    </el-table>
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getGoodsList"
    />
    <div slot="footer" class="dialog-footer">
      <el-button size="mini" type="primary" @click="sureChooseGoods">确定</el-button>
      <el-button size="mini" @click="cancelChooseGoods">取消</el-button>
    </div>
  </el-dialog>
</template>

<script>
  import {listGoods} from "@/api/system/goods";

  export default {
    props: {
      goodsStandard: {
        type: Boolean,
        default() {
          return true
        }
      },
      multiple: {
        type: Boolean,
        default() {
          return false
        }
      }
    },
    filters: {
      filterGoodsStatus(value) {
        return {
          1: "正常",
          2: "限时抢购",
          3: "拼团",
          4: "下架"
        }[value];
      }
    },
    data() {
      return {
        showSearch: true,
        openChooseGoods: false,
        // 总条数
        total: 0,
        // 产品表格数据
        goodsList: [],
        loading: false,
        // 查询参数
        queryParams: {
          pageNum: 1,
          pageSize: 10,
          goodsName: null
        },
        goodsInfo: null, // 展示选择的商品详情
        currentRow: null, // 选择的商品
        multipleData: [] //多选的数据
      }
    },

    methods: {
      show() {
        this.openChooseGoods = true
        this.getGoodsList();
      },
      sureChooseGoods() {
        // 单选
        if (!this.multiple) {
          if (!this.currentRow) {
            this.msgError("请选择商品");
            return;
          }
          this.goodsInfo = [{
            ...this.currentRow
          }]
        } else {
          // 多选
          this.goodsInfo = this.multipleData
        }
        this.openChooseGoods = false;
        
        this.$emit('selected', this.goodsInfo)
      },

      handleSelectionChange(res) {
        this.multipleData = res
      },
      
      cancelChooseGoods() {
        this.currentRow = null;
        this.goodsInfo = null;
        this.openChooseGoods = false;
        this.$refs.singleTable.setCurrentRow(null);
      },

      /** 搜索按钮操作 */
      handleQuery() {
        this.queryParams.pageNum = 1;
        this.getGoodsList();
      },
      /** 重置按钮操作 */
      resetQuery() {
        this.resetForm("queryForm");
        this.handleQuery();
      },

      /** 查询产品列表 */
      getGoodsList() {
        this.loading = true;
        listGoods(this.queryParams).then(response => {
          if (this.goodsStandard) {
            let arr = []
            response.rows.forEach(item => {
              if (item.standards) {
                item.standards = item.standards.map(standard => {
                  standard.goodsName = item.goodsName
                  standard.goodsCover = item.goodsCover.split(",")
                  standard.createTime = item.createTime
                  standard.goodsNumber = item.goodsNumber
                  standard.goodsStatus = item.goodsStatus
                  standard.goodsId = item.id
                  return standard
                })
                arr = arr.concat(item.standards)
              }
            })
            this.goodsList = arr
          } else {
            this.goodsList = response.rows.map(item => {
              item.goodsCover = item.goodsCover.split(",");
              return item;
            });
          }
          this.total = response.total;
          this.loading = false;
        });
      },

      chooseGoods() {
        this.openChooseGoods = true;
      },

      handleCurrentChange(val) {
        this.currentRow = {
          ...val,
          goodsId: val.id || val.goodsId
        };
        delete this.currentRow.id;
      }
    }
  }
</script>

<style lang="scss">
  .goods_wrapper_extend {
    background: #eee;
    margin-left: 120px;
    margin-bottom: 20px;
  }
</style>
